package com.rongma.waterdrop.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.rongma.waterdrop.R;
import com.rongma.waterdrop.adapter.ViewPagerAdapter;
import com.rongma.waterdrop.fragment.top.TodayFragment;
import com.rongma.waterdrop.fragment.top.TomatoFragment;

import java.util.ArrayList;


/**
 * Created by RongMa on 16/5/24.
 */
public class WaterDropFragment extends Fragment implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    private ViewPager contentPager;
    private RadioGroup rgTopNavigation;
    private TodayFragment todayFragment;
    private TomatoFragment tomatoFragment;
    private ViewPagerAdapter viewPagerAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.bottom_waterdrop_layout,null);
        contentPager = (ViewPager) view.findViewById(R.id.main_top_pager);
        rgTopNavigation = (RadioGroup) view.findViewById(R.id.rg_top);

        rgTopNavigation.setOnCheckedChangeListener(this);
        addFragmentToList();
        return view;
    }



    private void addFragmentToList() {
        todayFragment = new TodayFragment();
        tomatoFragment = new TomatoFragment();
        ArrayList<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(todayFragment);
        fragments.add(tomatoFragment);
        viewPagerAdapter = new ViewPagerAdapter(getFragmentManager(),fragments);
        contentPager.setAdapter(viewPagerAdapter);
        contentPager.addOnPageChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId)
        {
            case R.id.rb_today:
                contentPager.setCurrentItem(0);
                break;
            case R.id.rb_tomato:
                contentPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position)
        {
            case 0:
                rgTopNavigation.check(R.id.rb_today);
                break;
            case 1:
                rgTopNavigation.check(R.id.rb_tomato);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
